package com.jams.goodox.activity.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.holder.Holder;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.home.HomeActivity;

public class GuideBannerHolder implements Holder<Integer>, View.OnClickListener {
    private ImageView imageView;
    private ImageView img_bottom;
    private float mCurrentPosX;
    private float mCurrentPosY;

    private float mPosX;
    private float mPosY;

    private Context context;

    @Override
    public View createView(Context context) {
        //你可以通过layout文件来创建，也可以像我一样用代码创建，不一定是Image，任何控件都可以进行翻页
        View view = LayoutInflater.from(context).inflate(R.layout.guide_img_banner, null);
        imageView = (ImageView) view.findViewById(R.id.img);
        img_bottom = (ImageView) view.findViewById(R.id.img_bottom);
        imageView.setLongClickable(true);
        img_bottom.setOnClickListener(this);
        this.context = context;
        return view;
    }

    @Override
    public void UpdateUI(final Context context, int position, Integer data) {
        // 滑到最后一张图片
        if (position == context.getResources().obtainTypedArray(R.array.guide_images).length() - 1) {
            img_bottom.setVisibility(View.VISIBLE);
            imageView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            mPosX = event.getX();
                            mPosY = event.getY();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            mCurrentPosX = event.getX();
                            mCurrentPosY = event.getY();
                            if (mCurrentPosX - mPosX > 0 && Math.abs(mCurrentPosY - mPosY) < 10) {
                                Log.e("", "向右");
                            } else if (mCurrentPosX - mPosX < 0 && Math.abs(mCurrentPosY - mPosY) < 10) {
                                Log.e("", "向左");
                                openActivity(context);
                            } else if (mCurrentPosY - mPosY > 0 && Math.abs(mCurrentPosX - mPosX) < 10)
                                Log.e("", "向下");
                            else if (mCurrentPosY - mPosY < 0 && Math.abs(mCurrentPosX - mPosX) < 10)
                                Log.e("", "向上");
                            break;
                    }
                    return false;
                }
            });

        } else {
            imageView.setOnTouchListener(null);
        }

        imageView.setImageResource(data);
    }

    @Override
    public void onClick(View v) {
        openActivity(context);
    }

    private void openActivity(Context context) {
        if (!TextUtils.isEmpty(SePref.getToken())) {
            context.startActivity(new Intent(context,HomeActivity.class));
            ((BaseActivity) context).finish();
            //无公司id，表示登录未选择园区，进入园区选择
        } else {
            context.startActivity(new Intent(context,LoginActivity.class));
            ((BaseActivity) context).finish();
        }
    }
}
